package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.MqOpsService;
import com.rabbitmq.client.Channel;
import io.micrometer.common.util.StringUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author：yyj
 * @Package：com.atguigu.tingshu.account.consumer
 * @Project：tingshu_parent240829
 * @name：UserAccountReceiver
 * @Date：2025/1/14 2:37
 * @Filename：UserAccountReceiver
 */
@Component
@Slf4j
public class SearchInfoReceiver {
    @Autowired
    private MqOpsService mqOpsService;

    //消息消费

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER,durable = "true",exclusive = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            key = MqConst.ROUTING_ALBUM_UPPER))
    @SneakyThrows
    public void listenOnSaleAlbum(String content, Message message, Channel channel){

        int a=10;
        log.info("搜索微服务从指定队列中接收到上有专辑微服务传的数据:{}完成专辑上架",content);
        //1.获取消息进行判断
        if(StringUtils.isEmpty(content)){
            return;
        }
        //2.消费消息
        mqOpsService.listenOnSaleAlbum(content);

        //获取唯一标识
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //手动应答
        channel.basicAck(deliveryTag,false);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER, durable = "true", exclusive = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM),
            key = MqConst.ROUTING_ALBUM_LOWER))
    @SneakyThrows   // 可以绕开编译期间的异常 等运行时发现异常才抛出
    public void listenOffSaleAlbum(String content, Message message, Channel channel) {

        log.info("搜索微服务从指定队列中监听到上游专辑微服务的消息：{}完成专辑下架", content);
        // 1.获取消息，进行判断
        if (StringUtils.isEmpty(content)) {
            return;
        }

        // 2.消费消息
        mqOpsService.listenOffSaleAlbum(content);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 3.手动应答
        channel.basicAck(deliveryTag, false);


    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConst.QUEUE_ALBUM_ES_STAT_UPDATE, durable = "true", exclusive = "false"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM_ES_STAT_UPDATE),
            key = MqConst.ROUTING_ALBUM_ES_STAT_UPDATE))
    @SneakyThrows   // 可以绕开编译期间的异常 等运行时发现异常才抛出
    public void listenAlbumStatUpdate(String content, Message message, Channel channel) {

        log.info("搜索微服务从指定队列中监听到上游专辑微服务的消息：{}完成专辑下架", content);
        // 1.获取消息，进行判断
        if (StringUtils.isEmpty(content)) {
            return;
        }

        // 2.消费消息
        mqOpsService.updateAlbumStat(content);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        // 3.手动应答
        channel.basicAck(deliveryTag, false);


    }
}
